package cc.loac.heba.controller;

import cc.loac.heba.service.HeiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HeiController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    HeiService heiService;

    @GetMapping("/heis")
    @ResponseBody
    public String getHeis(@RequestParam("email") String email,
                         @RequestParam(value = "schoolName", required = false) String schoolName,
                         HttpServletRequest request) {

        logger.info(email + ": heis");
        String token = request.getHeader("admin-token");
        return heiService.getHeis(email, schoolName, token);
    }

    @GetMapping("/hei")
    @ResponseBody
    public String getHeiById(@RequestParam("email") String email,
                         @RequestParam("id") String id,
                         HttpServletRequest request) {
        logger.info(email + ": getHei: " + id);
        String token = request.getHeader("admin-token");
        return heiService.getHeiById(email, id, token);
    }

    @DeleteMapping("/hei")
    @ResponseBody
    public String removeHei(@RequestParam("email") String email,
                             @RequestParam("heiId") String heiId,
                             HttpServletRequest request) {
        logger.info(email + ": removeHei: " + heiId);
        String token = request.getHeader("admin-token");
        return heiService.removeHei(email, heiId, token);
    }

    @GetMapping("/hei/getByEmail")
    @ResponseBody
    public String getHeiByEmail(@RequestParam("email") String email,
                             @RequestParam("byEmail") String byEmail,
                             HttpServletRequest request) {
        logger.info(email + ": getHeiByEmail: " + byEmail);
        String token = request.getHeader("admin-token");
        return heiService.getHeiByEmail(email, byEmail, token);
    }

    @PostMapping("/hei/add")
    @ResponseBody
    public String addHei(@RequestParam("email") String email,
                         @RequestParam("title") String title,
                         @RequestParam("content") String content,
                         MultipartRequest files,
                         HttpServletRequest request) {
        List<MultipartFile> myFiles = new ArrayList<>();
        files.getMultiFileMap().forEach((key, multipartFiles) -> {
            MultipartFile multipartFile = multipartFiles.get(0);
            myFiles.add(multipartFile);
        });

        logger.info(email + ": addHei, FileCount: " + myFiles.size());
        String token = request.getHeader("admin-token");
        return heiService.addHei(email, title, content, myFiles, token, "");
    }

    @PostMapping("/hei/update")
    @ResponseBody
    public String updateHei(@RequestParam("email") String email,
                            @RequestParam("title") String title,
                            @RequestParam("content") String content,
                            @RequestParam("heiId") String heiId,
                            MultipartRequest files,
                            HttpServletRequest request) {
        List<MultipartFile> myFiles = new ArrayList<>();
        files.getMultiFileMap().forEach((key, multipartFiles) -> {
            MultipartFile multipartFile = multipartFiles.get(0);
            myFiles.add(multipartFile);
        });

        logger.info(email + ": updateHei, FileCount: " + myFiles.size());
        String token = request.getHeader("admin-token");
        return heiService.addHei(email, title, content, myFiles.size() > 0 ? myFiles : null, token, heiId);
    }

    @PostMapping("/hei/updateNoImg")
    @ResponseBody
    public String updateNoImg(@RequestParam("email") String email,
                             @RequestParam("title") String title,
                             @RequestParam("content") String content,
                             @RequestParam("heiId") String heiId,
                             HttpServletRequest request) {
        logger.info(email + ": updateNoImg");
        String token = request.getHeader("admin-token");
        return heiService.updateHeiTitleAndContent(email, title, content, heiId, token);
    }

    @PostMapping("/hei/like")
    @ResponseBody
    public String addLike(@RequestParam("email") String email,
                          @RequestParam("id") String heiId,
                          HttpServletRequest request) {
        logger.info(email + ": addLike, heiId: " + heiId);
        String token = request.getHeader("admin-token");
        return heiService.addLike(email, Integer.parseInt(heiId), token);
    }

    @GetMapping("/hei/likeAndCollect")
    @ResponseBody
    public String getLikeAndCollect(@RequestParam("email") String email,
                          HttpServletRequest request) {
        logger.info(email + ": likeAndCollect");
        String token = request.getHeader("admin-token");
        return heiService.getLikeAndCollect(email, token);
    }

    @PostMapping("/hei/collect")
    @ResponseBody
    public String addCollect(@RequestParam("email") String email,
                             @RequestParam("id") String heiId,
                             HttpServletRequest request) {
        logger.info(email + ": addCollect, heiId: " + heiId);
        String token = request.getHeader("admin-token");
        return heiService.addCollect(email, Integer.parseInt(heiId), token);
    }

    @DeleteMapping("/hei/like")
    @ResponseBody
    public String deleteLike(@RequestParam("email") String email,
                             @RequestParam("id") String heiId,
                             HttpServletRequest request) {
        logger.info(email + ": deleteLike, heiId: " + heiId);
        String token = request.getHeader("admin-token");
        return heiService.deleteLike(email, Integer.parseInt(heiId), token);
    }

    @DeleteMapping("/hei/collect")
    @ResponseBody
    public String deleteCollect(@RequestParam("email") String email,
                                @RequestParam("id") String heiId,
                                HttpServletRequest request) {
        logger.info(email + ": deleteCollect, heiId: " + heiId);
        String token = request.getHeader("admin-token");
        return heiService.deleteCollect(email, Integer.parseInt(heiId), token);
    }

    @GetMapping("/hei/likeHei")
    @ResponseBody
    public String getLikeHei(@RequestParam("email") String email,
                             HttpServletRequest request) {
        logger.info(email + ": getLikeHei");
        String token = request.getHeader("admin-token");
        return heiService.getLikeHeiByEmail(email, token);
    }

    @GetMapping("/hei/collectHei")
    @ResponseBody
    public String getCollectHei(@RequestParam("email") String email,
                                HttpServletRequest request) {
        logger.info(email + ": getCollectHei");
        String token = request.getHeader("admin-token");
        return heiService.getCollectHeiByEmail(email, token);
    }

    @GetMapping("/hei/myCount")
    @ResponseBody
    public String getMyHeiCount(@RequestParam("email") String email,
                              HttpServletRequest request) {
        logger.info(email + ": getHeiCount");
        String token = request.getHeader("admin-token");
        return heiService.getHeiCount(email, token);
    }

    @DeleteMapping("/heiImage")
    @ResponseBody
    public String deleteHeiImageById(@RequestParam("email") String email,
                                    @RequestParam("heiImageId") String heiImageId,
                                    HttpServletRequest request) {
        logger.info(email + ": deleteHeiImageById");
        String token = request.getHeader("admin-token");
        return heiService.removeHeiImageById(email, heiImageId, token);
    }
}
